package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.CropsBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 农作物数据适配
 */
public class CpContentdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Boolean> arrayListBoolean;
    private List<CropsBean.VBean.ALLCROPSBean.CropssBean> crops;
    public ArrayList<Boolean> getArrayListBoolean() {
        return arrayListBoolean;
    }
    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setArrayListBoolean(ArrayList<Boolean> arrayListBoolean) {
        this.arrayListBoolean = arrayListBoolean;
    }
    private String title;
    public CpContentdapter(Context context, List<CropsBean.VBean.ALLCROPSBean.CropssBean> crops) {
        this.context = context;
        this.crops = crops;
    }

    public CpContentdapter(Context context, List<CropsBean.VBean.ALLCROPSBean.CropssBean> crops, String title) {
        this.context = context;
        this.crops = crops;
        this.title = title;
    }

    @Override
    public int getCount() {
        if (title!=null) {
            return crops.size()+1;
        }
        return crops.size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cp_content, parent, false);
            holder = new ViewHolder();
            holder.tv_cainame = (TextView) convertView.findViewById(R.id.tv_cainame);
            holder.iv_caiimg = (ImageView) convertView.findViewById(R.id.iv_caiimg);
            holder.iv_dyu = (ImageView) convertView.findViewById(R.id.iv_dyu);
            holder.rl_cp_content = (RelativeLayout) convertView.findViewById(R.id.rl_cp_content);
            holder.iv_cp_add = (ImageView) convertView.findViewById(R.id.iv_cp_add);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (title != null) {
            if (position==crops.size()){
                holder.rl_cp_content.setVisibility(View.GONE);
                holder.iv_cp_add.setVisibility(View.VISIBLE);
            }else{
                holder.iv_cp_add.setVisibility(View.GONE);
                holder.rl_cp_content.setVisibility(View.VISIBLE);
                if (getArrayListBoolean().get(position)){
                    holder.iv_dyu.setVisibility(View.VISIBLE);
                }else{
                    holder.iv_dyu.setVisibility(View.GONE);
                }
                holder.tv_cainame.setText(crops.get(position).getCropName());
                if(crops.get(position).getCropUrl()!=null&&crops.get(position).getCropUrl().length()!=0){
                    ImageLoader.getInstance().displayImage("http://"+ MyApplication.IP + UrlBean.IMAGES +crops.get(position).getCropUrl(),holder.iv_caiimg);
                }else{
                    holder.iv_caiimg.setImageResource(R.mipmap.qincai);
                }
//                Glide.with(context)
//                        .load("http://" + MyApplication.IP + UrlBean.IMAGES +crops.get(position).getCropUrl())
//                        .error(R.mipmap.qincai)
//                        .thumbnail(0.1f)
//                        .into(holder.iv_caiimg);
            }
            holder.iv_cp_add.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Message msg = handler.obtainMessage();
                    msg.what = v.getId();
                    msg.arg1 = position;
                    handler.sendMessage(msg);
                }
            });
        } else {
            holder.tv_cainame.setText(crops.get(position).getCropName());
            Glide.with(context)
                    .load("http://" + MyApplication.IP + UrlBean.IMAGES +crops.get(position).getCropUrl())
                    .error(R.mipmap.qincai)
                    .into(holder.iv_caiimg);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView tv_cainame;
        ImageView iv_caiimg;
        ImageView iv_dyu;
        RelativeLayout rl_cp_content;
        ImageView iv_cp_add;
    }
}
